//
//  XMGAddVc.m
//  weibo
//
//  Created by DP on 16/7/3.
//  Copyright © 2016年 DP. All rights reserved.
//

#import "XMGAddVc.h"
#import "XMGHeadView.h"
#import "XMGBottomView.h"
#import "XMGAddBackBottomView.h"
#import "XMGButton.h"
#import "XMGAddItem.h"
#import "XMGAddWordNVc.h"
#import "XMGAddWordVc.h"

@interface XMGAddVc ()
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIView *btnView;
@property (nonatomic, strong) NSArray *addArray;
/**word*/
@property (nonatomic, weak) XMGAddWordNVc *wordNVc;
@end


#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
@implementation XMGAddVc

- (XMGAddWordNVc *)wordNVc {
    if (!_wordNVc) {
        UIStoryboard *wordStoryboard =  [UIStoryboard storyboardWithName:@"XMGWord" bundle:[NSBundle mainBundle]];
        XMGAddWordNVc *wordNVc = [wordStoryboard instantiateInitialViewController];
        _wordNVc = wordNVc;
    }
    return _wordNVc;
}
- (NSArray *)addArray {
    if (!_addArray) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"image.plist" ofType:nil];
       NSArray *arr = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *arrM = [NSMutableArray array];
        for (NSDictionary *dict in arr) {
            XMGAddItem *item = [XMGAddItem addItemWithDict:dict];
            [arrM addObject:item];
        }
        _addArray = arrM;
    }
    return _addArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加遮盖(两层)
    UIView *coverViewB= [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    coverViewB.backgroundColor = [UIColor blackColor];
    coverViewB.alpha = 0.5;
    [self.view addSubview:coverViewB];
    UIView *coverViewT = [[UIView alloc]init];
      coverViewT.frame = CGRectMake(0, 20, ScreenW, ScreenH);
    coverViewT.backgroundColor = [UIColor blackColor];
    [self.view addSubview:coverViewT];
    //添加顶部视图
    XMGHeadView *headView = [[[NSBundle mainBundle]loadNibNamed:@"XMGHeadView" owner:nil options:nil]lastObject];
    self.headView = headView;
    [self.view addSubview:headView];
     //添加Btn
    [self addBtn];
    
    //添加底部视图
    self.bottomView = [[[NSBundle mainBundle]loadNibNamed:@"XMGBottomView" owner:nil options:nil]lastObject];
    [self.view addSubview:self.bottomView];
    //添加手势
    UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [self.view addGestureRecognizer:tap];
    
    //接收通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(addBtnClick) name:@"addBtnClick" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tap) name:@"rightCancelItemClick" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(rightBackItemClick) name:@"rightBackItemClick" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tap) name:@"cancelItemClick" object:nil];
    
  }
//添加Btn
- (void)addBtn {
    UIView *btnView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenH, ScreenW *2, ScreenH -300)];
    self.btnView = btnView;
    //九宫格布局
    for (int i = 0; i <self.addArray.count; i++) {
        XMGButton *btn = [[XMGButton alloc]init];
        XMGAddItem *item = self.addArray[i];
        [btn setImage:[UIImage imageNamed:item.picture] forState:UIControlStateNormal];
        [btn setTitle:item.name forState:UIControlStateNormal];

        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i;
        int maxCol = 6;
        CGFloat btnW = 100;
        CGFloat btnH = 150;
        CGFloat space= (ScreenW *2 - maxCol *btnW) / (maxCol+1);
        CGFloat btnX = space;
        CGFloat btnY = space;
        btnX += i % maxCol *(space + btnW);
        btnY += i / maxCol *(space + btnH);
        btn.frame = CGRectMake(btnX, btnY, btnW , btnH);
        [self.btnView addSubview:btn];
    }
    [self.view addSubview:self.btnView];

}
//btn点击
- (void)btnClick:(UIButton *)sender {

    if (sender.tag == 8)
    {
//        [self.bottomView removeFromSuperview];
        self.bottomView = [[[NSBundle mainBundle]loadNibNamed:@"XMGAddBackBottomView" owner:nil options:nil]lastObject];
        [self.view addSubview:self.bottomView];
        
        [UIView animateWithDuration:0.5 animations:^{
            self.btnView.frame = CGRectMake(-ScreenW, 250, ScreenW *2, ScreenH - 300);
        }];
        self.tabBarController.tabBar.hidden = YES;
        
    }else if (sender.tag == 0)
    {
      
        NSLog(@"%@",[self.navigationController class]);
        [self.navigationController pushViewController:self.wordNVc animated:YES];
    
    }
    
}

//点击手势调用
- (void)tap {
     self.tabBarController.tabBar.hidden = NO;
    //隐藏btnView
    [UIView animateWithDuration:0.5 animations:^{
        if (self.btnView.frame.origin.x == 0) {
            self.btnView.frame = CGRectMake(0, ScreenH , ScreenW *2, ScreenH - 300);
        }else {
            self.btnView.frame = CGRectMake(-ScreenW, ScreenH , ScreenW *2, ScreenH - 300);
        }
    } completion:^(BOOL finished) {
        self.btnView.frame = CGRectMake(0, ScreenH , ScreenW *2, ScreenH - 300);
        [self.view removeFromSuperview];
        [self.bottomView removeFromSuperview];

    }];
}

//弹出btnView
- (void)addBtnClick {
    [UIView animateWithDuration:0.5 animations:^{
    self.headView.alpha = 0.8;
    self.btnView.frame = CGRectMake(0, 250, ScreenW *2, ScreenH - 300);
}];
}
- (void)rightBackItemClick {
    [self addBtnClick];
    [self.bottomView removeFromSuperview];
    self.bottomView = [[[NSBundle mainBundle]loadNibNamed:@"XMGBottomView" owner:nil options:nil]lastObject];
    [self.view addSubview:self.bottomView];
    
}
//移除监听者
- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


@end
